package com.pickgliss.ui.controls.container
{
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	public class SimpleTileList extends BoxContainer
	{
		public static const LIST_ITEM_CLICK : String = "LIST_ITEM_CLICK";
		public var startPos:Point = new Point(0,0);
		
		public static const ALIGN_LEFT:String = "align_left";
		public static const ALIGN_RIGHT:String = "align_right";
		private var _align:String;
		
		public function get align():String
		{
			return _align;
		}

		public function set align(value:String):void
		{
			_align = value;
		}

		public function set column(value:int):void
		{
			_column = value;
			refreshChildPos();
		}
		public function SimpleTileList(columnNum:int=1, align:String = SimpleTileList.ALIGN_LEFT)
		{
			super();
			_column = columnNum;
			_align = align;
		}

		protected var _column:int;
		protected var _hSpace:Number;
		protected var _rowNum:int;
		protected var _vSpace:Number;
		private var _selectedIndex:int;
		private var _selectedItem:DisplayObject;
		
		public function get selectedItem():DisplayObject
		{
			return _selectedItem;
		}

		public function get selectedIndex():int
		{
			return _selectedIndex;
		}

		public function set selectedIndex(value:int):void
		{
			if(_selectedIndex==value)return;
			_selectedIndex = value;
			_selectedItem = getChildAt(_selectedIndex);
		}
		
		public function get hSpace():Number
		{
			return _hSpace;
		}

		public function set hSpace(value:Number):void
		{
			_hSpace = value;
			onProppertiesUpdate();
		}

		public function get vSpace():Number
		{
			return _vSpace;
		}

		public function set vSpace(value:Number):void
		{
			_vSpace = value;
			onProppertiesUpdate();
		}
		
		override public function addChild(child:DisplayObject):DisplayObject{
			child.addEventListener(MouseEvent.CLICK,__itemClick);
			super.addChild(child);
			return child;
		}
		
		private function __itemClick(e:MouseEvent):void{
			var item:DisplayObject = e.currentTarget as DisplayObject;
			if(item.parent === this)
			{
				_selectedIndex = getChildIndex(item);
				_selectedItem = item;
				dispatchEvent(new Event(LIST_ITEM_CLICK));
			}
			else
			{
				_selectedIndex = -1;
				_selectedItem = null;
			}
		}
		
		override protected function arrange():void
		{
			caculateRows();
			var n:int = 0;
			var posX:int = startPos.x;
			var posY:int = startPos.y;
			var tempWidth:int = 0;
			var tempHeight:int = 0;
			for(var i:int = 0; i<_rowNum; i++)
			{
				var maxHeight:int = 0;
				for(var j:int = 0; j<_column; j++)
				{
					var ch:DisplayObject = getChildAt(n++);
					if(_align == SimpleTileList.ALIGN_LEFT)
					{
						ch.x = posX;
					}
					else if(_align == SimpleTileList.ALIGN_RIGHT)
					{
						ch.x = width - posX - ObjectUtils.getObjectHeight(ch);
					}
					ch.y = posY;
					tempWidth = Math.max(tempWidth,posX + ObjectUtils.getObjectWidth(ch));
					tempHeight = Math.max(tempHeight,posY+ObjectUtils.getObjectHeight(ch));
					posX += (ObjectUtils.getObjectWidth(ch) + _hSpace);
					if(maxHeight < ObjectUtils.getObjectHeight(ch))
					{
						maxHeight = ObjectUtils.getObjectHeight(ch);
					}
					if(n>=numChildren)
					{
						changeSize(tempWidth,tempHeight);
						return;
					}
				}
				posX = startPos.x;
				posY += (maxHeight + _vSpace);
			}
			changeSize(tempWidth,tempHeight);
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function changeSize(w:int,h:int):void
		{
			if(w != _width || h != _height)
			{
				width = w;
				height = h;
			}
		}
		
		private function caculateRows():void
		{
			_rowNum = Math.ceil(numChildren/_column);
		}
		
		override public function dispose():void{
			for (var i:int = 0; i < numChildren; i++)
			{
				var item:DisplayObject = getChildAt(i) as DisplayObject
				item.removeEventListener(MouseEvent.CLICK,__itemClick);
			}
			super.dispose();
		}
	}
}